home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: mixing enums and integers
- Date: Fri, 05 Jan 96 21:40:29 GMT
- Organization: none
- Message-ID: <820878029snz@genesis.demon.co.uk>
- References: <4cij6k$dvb@yrkpa.kias.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4cij6k$dvb@yrkpa.kias.com>
- glynis@yrkpa.kias.com "John Flinchbaugh" writes:
-
- >the faq states that mixing enums and ints is legal, but poor style.
- >
- >i'd like to use enums to organize my constants, like this:
- >
- > enum DIRECT {UP=72,LEFT=75,RIGHT=77,DOWN=80};
- > int key;
- > getch(); /* dos code to get the second byte of 2-byte */
- > key=getch(); /* key codes from keyboard */
- > if (key==UP) printf("up");
- > ...
- >
- >now this should work, but it's poor style, right?
-
- Not in my opinion. You make a clear distinction between an enum type and
- the identifiers in an enumerator list. If you write
-
- enum DIRECT direct;
-
- then direct has type enum DIRECT which is compatible with an
- implementation-defined integer type. It is probably bad style to mix
- direct with ints. However the enumerator list identifiers such as UP, LEFT
- etc. have type int according to the standard; it is quite reasonable
- to assign and compare these to ints. IMHO it is in many cases better
- style to define constants with enums like this than to use #defines.
- There's an example of enums being used like this in section 9.1 of the FAQ.
-
- >would casting it be more proper:
- >
- > if ((enum DIRECT) key==up) ...
- UP
-
- Not IMHO.
-
- >also, can you typedef enums? if so, how?
-
- The same way as you typedef anything else (in particular structures and
- unions).
-
- typedef enum {UP=72,LEFT=75,RIGHT=77,DOWN=80} DIRECT;
-
- or
-
- typedef enum Direct {UP=72,LEFT=75,RIGHT=77,DOWN=80} DIRECT;
-
- or
-
- enum Direct {UP=72,LEFT=75,RIGHT=77,DOWN=80};
- typedef enum Direct DIRECT;
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-